﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;
using System.Data.Common;

namespace TerryDong.Data
{
    public static class DataUtility
    {
        /// <summary>
        /// get the value is not null properties
        /// </summary>
        /// <typeparam name="T">data model class</typeparam>
        /// <param name="model">data model object</param>
        /// <returns></returns>
        public static List<PropertyInfo> GetNotNullProperties<T>(T model)        
        {
            PropertyInfo[] properties = model.GetType().GetProperties();

            List<PropertyInfo> returnValue = new List<PropertyInfo>();
            foreach (PropertyInfo p in properties)
            {
                if (p.GetValue(model,null)!=null)
                {
                    returnValue.Add(p);
                }
            }
            return returnValue;
        }

        public static string GetPropertyName<T>(T model)
        { 
           // PropertyInfo p=model.GetType()
            return string.Empty;
        }

        public static PropertyInfo GetNotNullPropertyName<T>(T model)
        {
            List<PropertyInfo> props = GetNotNullProperties<T>(model);
            if (props.Count==1)
            {
                return props[0];
            }
            else
            {
                throw new ArgumentException("the input argument has no or more than one not null properties");
            }
        }

        /// <summary>
        /// compare model's property name and input fileds ,compared with name.Tolower() 
        /// </summary>
        /// <typeparam name="T">data model class</typeparam>
        /// <param name="model">data model object</param>
        /// <param name="toValidFields">string array to verified.</param>
        /// <returns>return the matched property names.</returns>
        public static List<string> GetValidPropertyName<T>(T model,string[] toValidFields)
        {
            List<string> returnValue = new List<string>();
            foreach (string s in toValidFields)
            {
                var result = from p in model.GetType().GetProperties()
                             where p.Name.ToLower() == s.ToLower()
                             select p.Name;
                if (result!=null&& result.ToList<string>().Count==1)
                {
                    returnValue.Add(result.ToList<string>()[0]);
                }
            }                    
            return returnValue;
            
        }

        //public static List<string> GetContainedProperties<T>(T model, string[] toValidFields)
        //{
        //    List<string> returnValue = new List<string>();
        //    foreach (string s in toValidFields)
        //    {
        //        var result = from p in model.GetType().GetProperties()
        //                     where s.ToLower().Contains(p.Name.ToLower())
        //                     select p.Name;
        //        if (result != null && result.ToList<string>().Count == 1)
        //        {
        //            s.Replace(
        //        }
        //    }
        //}

        /// <summary>
        /// Set Model properties' Value By DataReader
        /// </summary>
        /// <typeparam name="T">data model class</typeparam>
        /// <param name="dataReader">data model object</param>
        /// <returns>model with property value</returns>
        public static T SetModelValueByDataReader<T>(T model,DbDataReader dataReader)
        {
            List<string> columns=GetDataReaderColumnsName(dataReader);
            PropertyInfo[] properties = model.GetType().GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if (columns.Contains(property.Name)&&!Convert.IsDBNull(dataReader[property.Name]))
                {
                    property.SetValue(model, dataReader[property.Name], null);
                }
            }
            
            return model;
        }

        public static List<string> GetDataReaderColumnsName(DbDataReader dataReader)
        {
            List<string> returnValue = new List<string>();
            for (int i = 0; i < dataReader.FieldCount; i++)
            {
                returnValue.Add(dataReader.GetName(i));
            }
            return returnValue;
        }

        public static T GenerateParameters<T, V>(T parameterType, V entity)
        {
            return Activator.CreateInstance<T>();
        }

        //生成参数

        //生成sql

        //生成查询条件
        //"order_status=0 and cust_id=2"
        //"order_status=0"

        //不同数据库的区分
    }
}
